// Copyright (C) 2008 Taylor L. Riche <riche@cs.utexas.edu>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

package appcomp.pathfinder;

import static org.junit.Assert.*;
import java.net.URI;
import java.util.ArrayList;
import appcomp.entities.*;
import core.Entity;
import utility.Pair;

import org.junit.Test;

public class PathTest {
	
	@Test
	public void testGetSlack() {
		Type testType = Type.create("testType", URI.create("test"), "test");
		Port op1in1 = Port.create("op1in1", "handler1", null, Port.Direction.IN, testType);
		Port op1out1 = Port.create("op1out1", null, null, Port.Direction.OUT, testType);
		ArrayList<Port> portList = new ArrayList<Port>();
		portList.add(op1in1);
		portList.add(op1out1);
		Operator op1 = Operator.create("op1", null, portList, null);
		Port op2in1 = Port.create("op2in1", "handler2", null, Port.Direction.IN, testType);
		Port op2out1 = Port.create("op2out1", null, null, Port.Direction.OUT, testType);
		portList = new ArrayList<Port>();
		portList.add(op2in1);
		portList.add(op2out1);
		Operator op2 = Operator.create("op2", null, portList, null);
		Path p = new Path();
		Connector con1 = Connector.create(new Pair<String, String>("op1", "op1out1"), new Pair<String, String>("op2", "op2in1"));
		con1.setIn(op1);
		con1.setOut(op2);
		p.addHop(con1);
		if(p.getSlack(100) != 90) {
			fail();
		}
		Port op3in1 = Port.create("op3in1", "handler3", null, Port.Direction.IN, testType);
		Port op3out1 = Port.create("op3out1", null, null, Port.Direction.OUT, testType);
		portList = new ArrayList<Port>();
		portList.add(op3in1);
		portList.add(op3out1);
		Operator op3 = Operator.create("op3", null, portList, null);
		Connector con2 = Connector.create(new Pair<String, String>("op2", "op2out1"), new Pair<String, String>("op3", "op3in1"));
		con2.setIn(op2);
		con2.setOut(op3);
		p.addHop(con2);
		if(p.getSlack(100) != 80) {
			fail();
		}
	}

	@Test
	public void testToString() {
		Path p = new Path();
		Connector conn = Connector.create(new Pair<String, String>("testOp1", "testPort1"), new Pair<String, String>("testOp2", "testPort2"));
		Connector conn2 = Connector.create(new Pair<String, String>("testOp2", "testPort2"), new Pair<String, String>("testOp3", "testPort3"));
		p.addHop(conn);
		if(!p.toString().equals("testOp1.testPort1->testOp2.testPort2::X")) {
			fail();
		}
		p.addHop(conn2);
		if(!p.toString().equals("testOp1.testPort1->testOp2.testPort2::testOp2.testPort2->testOp3.testPort3::X")) {
			fail();
		}
	}

	@Test
	public void testAddHop() {
		Path p = new Path();
		Connector conn = Connector.create(new Pair<String, String>("testOp1", "testPort1"), new Pair<String, String>("testOp2", "testPort2"));
		Connector conn2 = Connector.create(new Pair<String, String>("testOp2", "testPort2"), new Pair<String, String>("testOp3", "testPort3"));
		if (p.getLength() != 0) {
			fail();
		}
		p.addHop(conn);
		if (p.getLength() != 2) { 
			fail();
		}	
		p.addHop(conn2);
		if (p.getLength() != 3) {
			fail();
		}
	}
	
}
